package com.aispeech.imgany.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

import com.aispeech.imgany.MyApplication;
import com.aispeech.imgany.R;
import com.androidyuan.lib.screenshot.Shotter;

import java.io.File;

import static com.androidyuan.lib.screenshot.ScreenShotActivity.REQUEST_MEDIA_PROJECTION;


public class TestAnimator extends Activity implements View.OnClickListener {
    private final static String TAG = TestAnimator.class.getSimpleName();
    private Button bt_screenshot;
    private View content_animator;
    private ImageView iv_screenshot;
    private View view_left;
    private View view_right;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        initeVIew();

//        File file = new File("/storage/emulated/0/DCIM/Screenshots/Screenshot_2018-03-13-17-52-20-120_com.aispeech.imgany.png");

        finish();
    }

    private MediaProjection mediaProjection;
    private MediaProjectionManager mediaProjectionManager;
    private int mScreenDensity;


    private void initeVIew() {
        bt_screenshot = (Button) findViewById(R.id.bt_screenshot);
        bt_screenshot.setOnClickListener(this);
        content_animator = findViewById(R.id.animator_content);
        iv_screenshot = (ImageView) findViewById(R.id.iv_screenshot);
        view_left = findViewById(R.id.left_view);
        view_right = findViewById(R.id.right_view);

//        iv_screenshot.setVisibility(View.VISIBLE);
//        Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Screenshots/Screenshot_2018-03-13-17-52-20-120_com.aispeech.imgany.png");
//        iv_screenshot.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_screenshot:
//                shotScreen();
                showDataContent();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ScreenShotUtil.getInstance().dismissDataContent();
    }

    private void showDataContent() {
        ScreenShotUtil.getInstance().initDataContent(this);
        ScreenShotUtil.getInstance().showDataContent();
    }

    private void shotScreen() {
        AnimatorSet set = new AnimatorSet();
        //screen shot animator
        ObjectAnimator animator_left = ObjectAnimator.ofFloat(view_left, "translationX", -MyApplication.height / 2, 0, -MyApplication.height / 2);
        ObjectAnimator animator_right = ObjectAnimator.ofFloat(view_right, "translationX", MyApplication.height / 2, 0, MyApplication.height / 2);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                content_animator.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                content_animator.setVisibility(View.GONE);
                requestCapturePermission();
            }

        });
        set.play(animator_left).with(animator_right);
        set.setDuration(800);
        set.start();
    }

    public void requestCapturePermission() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            //5.0 之后才允许使用屏幕截图
            return;
        }

        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(
                mediaProjectionManager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_MEDIA_PROJECTION: {
                if (resultCode == -1 && data != null) {
                    Shotter shotter = new Shotter(TestAnimator.this, data);
                    shotter.startScreenShot(new Shotter.OnShotListener() {
                        @Override
                        public void onFinish(String mpath) {
//                            imgpath mpath
                            if (TextUtils.isEmpty(mpath)) {
                                Log.d(TAG, "shot err ! no file save !");
                            } else {
                                Log.d(TAG, "shot finish! : " + mpath);

                                setSreenShotView(mpath);

                            }
                        }
                    });
                }
            }
        }
    }

    private void setSreenShotView(String mpath) {
        Bitmap bitmap = BitmapFactory.decodeFile(mpath);
        iv_screenshot.setImageBitmap(bitmap);
        iv_screenshot.setVisibility(View.VISIBLE);

        sendToRequestAnimator();
    }

    private void sendToRequestAnimator() {
        Log.d(TAG, "iv X:" + iv_screenshot.getTranslationX());
        Log.d(TAG, "iv Y:" + iv_screenshot.getTranslationY());
        float locateX = (float) (MyApplication.width * 2.5 / 4);
        float locateY = MyApplication.height * 3 / 4;

        AnimatorSet set = new AnimatorSet();
        ObjectAnimator animator_TRX = ObjectAnimator.ofFloat(iv_screenshot, "translationX", 0f, locateY);
        ObjectAnimator animator_TRY = ObjectAnimator.ofFloat(iv_screenshot, "translationY", 0f, locateX);
        ObjectAnimator animator_SX = ObjectAnimator.ofFloat(iv_screenshot, "scaleX", 1f, 1 / 4f);
        ObjectAnimator animator_SY = ObjectAnimator.ofFloat(iv_screenshot, "scaleY", 1f, 1 / 4f);
        iv_screenshot.setPivotX(0);
        iv_screenshot.setPivotY(0);
        set.playTogether(animator_TRX, animator_TRY, animator_SX, animator_SY);
        set.setDuration(600);

        ObjectAnimator anmator_TRX_repect = ObjectAnimator.ofFloat(iv_screenshot, "translationX", locateY, locateY + 20, locateY, locateY + 20, locateY, locateY + 20, locateY);
        anmator_TRX_repect.setDuration(1500);
        anmator_TRX_repect.setStartDelay(200);

        ObjectAnimator anmator_TRX_dismiss = ObjectAnimator.ofFloat(iv_screenshot, "translationX", locateY, MyApplication.height);
        anmator_TRX_dismiss.setInterpolator(new AccelerateInterpolator());
        anmator_TRX_dismiss.setDuration(600);
        anmator_TRX_dismiss.setStartDelay(800);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially(set, anmator_TRX_repect,anmator_TRX_dismiss);
        animatorSet.start();

    }
}
